package com.forum.module.blog.service.favorite;

import com.alibaba.nacos.shaded.com.google.common.annotations.VisibleForTesting;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forum.framework.common.exception.util.ServiceExceptionUtils;
import com.forum.framework.common.pojo.PageResult;
import com.forum.framework.common.util.object.BeanUtils;
import com.forum.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.forum.framework.security.core.LoginUser;
import com.forum.framework.security.core.util.SecurityUtils;
import com.forum.module.blog.controller.favorite.vo.FavoritePageReqVO;
import com.forum.module.blog.controller.favorite.vo.FavoriteSavaReqVO;
import com.forum.module.blog.dal.dataobject.article.ArticleDO;
import com.forum.module.blog.dal.dataobject.favorite.FavoriteDO;
import com.forum.module.blog.dal.mysql.article.ArticleMapper;
import com.forum.module.blog.dal.mysql.favorite.FavoriteMapper;
import com.forum.module.blog.service.notification.NotificationService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.forum.module.system.enums.ErrorCodeConstants.FAVORITE_IS_EXISTS;
import static com.forum.module.system.enums.ErrorCodeConstants.FAVORITE_NOT_EXISTS;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2025/3/2 18:30
 */
@Service
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, FavoriteDO> implements FavoriteService {
    private final FavoriteMapper favoriteMapper;
    private final NotificationService notificationService;

    private final ArticleMapper articleMapper;

    public FavoriteServiceImpl(FavoriteMapper favoriteMapper,
                               NotificationService notificationService,
                               ArticleMapper articleMapper) {
        this.favoriteMapper = favoriteMapper;
        this.notificationService = notificationService;
        this.articleMapper = articleMapper;
    }

    @Override
    public Long createFavorite(FavoriteSavaReqVO reqVO) {
        FavoriteDO favoriteDO = BeanUtils.toBean(reqVO, FavoriteDO.class);
        Long loginUserId = SecurityUtils.getLoginUserId();
        favoriteDO.setUserId(loginUserId);

        if (!favoriteMapper.exists(new LambdaQueryWrapperX<FavoriteDO>()
                .eq(FavoriteDO::getArticleId, reqVO.getArticleId())
                .eq(FavoriteDO::getUserId, loginUserId))) {
            favoriteMapper.insert(favoriteDO);

            ArticleDO articleDO = articleMapper.selectById(reqVO.getArticleId());
            LoginUser loginUser = SecurityUtils.getLoginUser();
            assert loginUser != null;
            String message = "用户:" +
                    loginUser.getInfo().get("nickname") +
                    "，收藏了你的文章！";
            notificationService.sendMessage(articleDO.getUserId(), 3, message);
        } else {
            throw ServiceExceptionUtils.exception(FAVORITE_IS_EXISTS);
        }
        return favoriteDO.getId();
    }

    @Override
    public void updateFavorite(FavoriteSavaReqVO reqVO) {
        FavoriteDO updateObj = BeanUtils.toBean(reqVO, FavoriteDO.class);
        favoriteMapper.updateById(updateObj);
    }

    @Override
    public void deleteById(Long id) {
        // 1. 校验收藏存在
        validateFavoriteExists(id);

        favoriteMapper.deleteById(id);
    }

    @Override
    public PageResult<FavoriteDO> getFavoritePage(FavoritePageReqVO pageReqVO) {
        return favoriteMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FavoriteDO> getByUserId(Long loginUserId) {
        return favoriteMapper.selectList(new LambdaQueryWrapperX<FavoriteDO>()
                .eqIfPresent(FavoriteDO::getUserId, loginUserId));
    }

    @VisibleForTesting
    FavoriteDO validateFavoriteExists(Long id) {
        if (id == null) {
            return null;
        }
        FavoriteDO favoriteDO = favoriteMapper.selectById(id);
        if (favoriteDO == null) {
            throw ServiceExceptionUtils.exception(FAVORITE_NOT_EXISTS);
        }
        return favoriteDO;
    }
}
